import React, { useState } from 'react';
import { Form, Input, Button, Card, message, Checkbox } from 'antd';
import './Login.css';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import { useNavigate } from 'react-router-dom';
import { useAuthStore } from '@/store/auth';
import { useUserStore } from '@/store/user';

const Login: React.FC = () => {
  const [loading, setLoading] = useState(false);
  // const [rememberMe, setRememberMe] = useState(false); // 暂时未使用，注释掉
  const navigate = useNavigate();
  const { login } = useAuthStore();
  const { getUserPermissions, clearPermissions } = useUserStore();

  const onFinish = async (values: { username: string; password: string }) => {
    try {
      setLoading(true);

      // 清除旧的权限数据
      clearPermissions();

      // 登录
      await login(values.username, values.password);

      // 获取用户权限数据
      await getUserPermissions();

      message.success('登录成功');
      navigate('/dashboard');
    } catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : '登录失败';
      message.error(errorMessage);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="login-container">
      <Card className="login-card" bordered={false}>
        <div style={{ textAlign: 'center', marginBottom: 32 }}>
          <h1 style={{ margin: 0, color: '#1890ff' }}>管理系统</h1>
          <p style={{ margin: '8px 0 0 0', color: '#666' }}>欢迎登录</p>
        </div>

        <Form
          name="login"
          onFinish={onFinish}
          autoComplete="off"
          size="large"
        >
          <Form.Item
            name="username"
            rules={[{ required: true, message: '请输入用户名!' }]}
          >
            <Input
              prefix={<UserOutlined />}
              placeholder="用户名"
            />
          </Form.Item>

          <Form.Item
            name="password"
            rules={[{ required: true, message: '请输入密码!' }]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="密码"
            />
          </Form.Item>

          <Form.Item>
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
              <Form.Item name="remember" valuePropName="checked" noStyle>
                <Checkbox>
                  记住我
                </Checkbox>
              </Form.Item>
              <a href="#" style={{ color: '#1890ff' }}>
                忘记密码？
              </a>
            </div>
          </Form.Item>

          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              loading={loading}
              style={{ width: '100%' }}
            >
              登录
            </Button>
          </Form.Item>
        </Form>

        <div style={{ textAlign: 'center', marginTop: 16 }}>
          <span style={{ color: '#666' }}>还没有账号？</span>
          <a href="#" style={{ color: '#1890ff', marginLeft: 4 }}>
            立即注册
          </a>
        </div>
      </Card>
    </div>
  );
};

export default Login;